/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace DslEditorPowerToy.Controls.VirtualTreeGrid
{
    internal static class TypeDescriptorHelper
    {
        #region Public Methods
        /// <summary>
        /// Returns either a custom descriptor for the component or null.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static PropertyDescriptor GetCustomDescriptor(object component, string propertyName)
        {
            //Check if item provides its own custom type descriptor
            if (component is ICustomTypeDescriptor)
            {
                //Get Runtime propertyDescriptor for this property
                return ((ICustomTypeDescriptor)component).GetProperties()[propertyName];
            }

            //Check if implements a Type Descriptor Provider pattern
            TypeDescriptionProvider descriptorProvider = TypeDescriptor.GetProvider(component);
            if (null != descriptorProvider)
            {
                ICustomTypeDescriptor customTypeDescriptor = descriptorProvider.GetTypeDescriptor(component);
                if (null != customTypeDescriptor)
                    return customTypeDescriptor.GetProperties()[propertyName];
            }

            return null;
        }
        #endregion
    }
}
